package com.example.cunion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cunion.entity.Advertisement;
import com.example.cunion.exception.CunionException;
import com.example.cunion.mapper.AdvertisementMapper;
import com.example.cunion.service.AdvertisementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
* @author Aero
* @description 针对表【advertisement(广告表)】的数据库操作Service实现
* @createDate 2023-10-22 23:43:49
*/
@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement>
    implements AdvertisementService{

    @Resource
    private AdvertisementMapper advertisementMapper;

    @Override
    public List<HashMap> searchUnCheckAdvertisementByPage(HashMap map) {
        List<HashMap> list = advertisementMapper.searchUnCheckAdvertisementByPage(map);
        return list;
    }

    @Override
    public List<HashMap> searchUnCheckAdminAdvertisementByPage(HashMap map) {
        List<HashMap> list = advertisementMapper.searchUnCheckAdminAdvertisementByPage(map);
        return list;
    }

    @Override
    public Integer agreeAdvertisement(String id) {
        Integer result = advertisementMapper.agreeAdvertisement(id);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        return result;
    }

    @Override
    public Integer rejectAdvertisement(String id) {
        Integer result = advertisementMapper.rejectAdvertisement(id);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        return result;
    }

    @Override
    public Integer addAdvertisement(HashMap map) {
        Integer result = advertisementMapper.addAdvertisement(map);
        if (result != 1){
            throw new CunionException("添加广告失败！");
        }
        return result;
    }

    @Override
    public Integer updateAdvertisementStatus(HashMap map) {
        Integer result = advertisementMapper.updateAdvertisementStatus(map);
        if (result != 1){
            throw new CunionException("审核失败！");
        }
        return result;
    }

    @Override
    public Integer addAdvertisement(Advertisement advertisement) {
        int insert = advertisementMapper.insert(advertisement);
        if (insert != 1){
            throw new CunionException("添加失败");
        }
        return insert;
    }

    @Override
    public Integer deletedAdvertisement(String advertisementId) {
        int result = advertisementMapper.deleteById(advertisementId);
        if (result != 1){
            throw new CunionException("删除失败！");
        }
        return result;
    }

    @Override
    public Integer updateAdvertisement(Advertisement advertisement) {
        int result = advertisementMapper.updateById(advertisement);
        if (result != 1){
            throw new CunionException("修改失败");
        }
        return result;
    }

    @Override
    public List<Advertisement> searchAdvertisement(Integer start, Integer length) {
        Page<Advertisement> page = new Page<>(start, length);
        QueryWrapper<Advertisement> QueryWrapper = new QueryWrapper<>();
        Page<Advertisement> selectPage = advertisementMapper.selectPage(page, QueryWrapper);
        List<Advertisement> list = selectPage.getRecords();
        return list;
    }
}




